import { menu } from "../../decorator";

const { ccclass, property } = cc._decorator;

@ccclass
@menu(__filename)
export default class initScrollView extends cc.Component {
    @property
    protected _target: cc.Node = null
    @property({
        type: cc.Node,
        tooltip: '默认是本节点'
    })
    get target() {
        if (this._target && !this._target.isValid) {
            this._target = null;
        }
        return this._target || this.node;
    }
    set target(value) {
        this._target = value;
    }


    @property
    private _itemPre: cc.Prefab = null;
    @property(cc.Prefab)
    get itemPre() { return this._itemPre; }
    set itemPre(value) {
        this._itemPre = value;

        if (this._itemPre) {
            this.itemComName = this._itemPre.name;
            this.itemComFun = 'init';
        } else {
            this.itemComName = '';
            this.itemComFun = '';
        }
    }

    @property({
        visible: function () {
            return !!this.initCom;
        },
        tooltip: '预制的脚本名字'
    })
    itemComName: string = '';

    @property({
        visible: function () {
            return !!this.initCom;
        },
        tooltip: '调用预制脚本的方法名'
    })
    itemComFun: string = '';

    private _pool: cc.NodePool = null;


    _getPool() {
        if (!this._pool) {
            this._pool = new cc.NodePool(this.itemComName);
        }
        return this._pool;
    }

    _getNode() {
        let node;
        if (this._getPool().size() > 0) {
            node = this._pool.get();
        } else {
            node = cc.instantiate(this.itemPre);
        }
        return node;
    }

    _putNode(node) {
        this._getPool().put(node);
    }

    sub(num: number) {
        const node = this.target || this.node;
        const content = node.getComponent(cc.ScrollView).content;

        const children = content.children;
        for (let i = 0; i < num; i++) {
            this._putNode(children[children.length - 1]);
        }
    }

    add(num: number, data?: any, handleCB?: Function) {
        const node = this.target || this.node;
        const content = node.getComponent(cc.ScrollView).content;

        const children = content.children;

        for (let index = 0; index < num; index++) {
            const node = this._getNode();
            const com = node.getComponent(this.itemComName);
            com[this.itemComFun](index, data, children.length);
            node.parent = content;

            handleCB && handleCB(com, index, data, index);
        }
    }

    reload(num: number, data?: any, handleCB?: Function) {
        const node = this.target || this.node;
        const content = node.getComponent(cc.ScrollView).content;

        const children = content.children;

        let index = 0;
        let com = null;
        for (; index < num; index++) {
            if (children[index]) {
                com = children[index].getComponent(this.itemComName);
                com[this.itemComFun](index, data, index);
            } else {
                const node = this._getNode();
                com = node.getComponent(this.itemComName);
                com[this.itemComFun](index, data, index);
                node.parent = content;
            }
            
            handleCB && handleCB(com, index, data, index);
        }

        for (let i = children.length - 1; i >= index; i--) {
            this._putNode(children[i]);
        }
    }

    init(num: number, data?: any, handleCB?: Function) {
        this.reload(num, data, handleCB);

        const node = this.target || this.node;
        const sv = node.getComponent(cc.ScrollView);
        sv.stopAutoScroll();
        sv.scrollToTop();
    }

    clear() {
        const node = this.target || this.node;
        const content = node.getComponent(cc.ScrollView).content;
        const children = content.children;
        for (let i = children.length - 1; i >= 0; i--) {
            this._putNode(children[i]);
        }
    }
}
